package com.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dk.data.Result;
import com.dk.data.ResultEnum;
import com.dk.pojo.goods.TbGoods;
import com.dk.pojo.goods.TbGoodsDesc;
import com.dk.pojo.item.TbItem;
import com.dk.pojo.item.TbItemCat;
import com.dk.pojo.seller.TbSeller;
import com.dk.service.goods.GoodsService;
import com.dk.service.item.ItemCatService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@RequestMapping("goods")
public class GoodsController {

    private final Logger logger = LoggerFactory.getLogger(GoodsController.class);

    @Reference
    private ItemCatService itemCatService;

    @Reference
    private GoodsService goodsService;

    @RequestMapping("toGoodsEdit")
    public String toGoodsEdit() {
        return "admin/goods_edit";
    }

    @RequestMapping("listItemCatByPid/{parentId}")
    @ResponseBody
    public Result listItemCatByPid(@PathVariable("parentId") Long parentId){

        try {
            Map<String, Object> map = itemCatService.listItemCatByPid(parentId);
            Result result = Result.response(ResultEnum.OK).setData(map);
            return result;
        }catch (Exception e) {
            return Result.response(ResultEnum.FAIL);
        }

    }


    @RequestMapping("saveGoods")
    @ResponseBody
    public Result saveGoods(TbGoods goods, TbGoodsDesc goodsDesc,String item,String skuList) {
        try {
            System.out.println(goodsDesc.getSpecificationItems());
            Subject subject = SecurityUtils.getSubject();
            TbSeller seller = (TbSeller) subject.getPrincipal();
            goods.setSellerId(seller.getSellerId());
            List<TbItem> tbItems = JSONObject.parseArray(item, TbItem.class);
            System.out.println(tbItems);
            List<String> skuStrings = JSONObject.parseArray(skuList, String.class);
//            System.out.println(strings);
            int i = goodsService.saveGoods(goods,goodsDesc,tbItems,skuStrings);

            return Result.response(ResultEnum.OK);
        } catch (Exception e) {
            logger.error("商品添加错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    @RequestMapping("listGoods")
    @ResponseBody
    public Result listGoods() {
        try {
            List<TbGoods> list = goodsService.listGoods();
            return Result.response(ResultEnum.OK).setData(list);
        } catch (Exception e) {
            logger.error("商品查询列表错误:" ,e);
            return Result.response(ResultEnum.FAIL);
        }
    }

}
